﻿using Shinva.LADModel.Abstractions;
using Shinva.Variables.Interface;
using Shinva.Variables.Models;
using Shinva.Variables.Type.bit;
using Shinva.Variables.Type.floating;
using Shinva.Variables.Type.integer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Shinva.LADModel.Type.Mathfunctions
{
    public class Add<T> : ElementNodeBase where T : PlcMemberBase
    {
        public T in1 { get; set; }
        public T in2 { get; set; }
        public T in3 { get; set; }

        public T in4 { get; set; }
        public T in5 { get; set; }
        public T in6 { get; set; }
        public T in7 { get; set; }
        public T in8 { get; set; }
        public T in9 { get; set; }

        public T OUT { get; set; } //实际为out

        private int _inCount = 2;
        public override PlcBool EN
        {
            get
            {
                return en;
            }
            set
            {
                en = value;
            }
        }
        public override PlcBool ENO
        {
            get
            {

                eno.Value = en.Value && AddValue();
                return eno;
            }
            set
            {
                eno = value;
            }
        }
        public Add(int inCount)
        {
            Name = "Add";
            _inCount = inCount;
            variableTableInterface.AddVariable("in1", StorageType.Input);
            variableTableInterface.AddVariable("in2", StorageType.Input);
            variableTableInterface.AddVariable("in3", StorageType.Input);
            variableTableInterface.AddVariable("in4", StorageType.Input);
            variableTableInterface.AddVariable("in5", StorageType.Input);
            variableTableInterface.AddVariable("in6", StorageType.Input);
            variableTableInterface.AddVariable("in7", StorageType.Input);
            variableTableInterface.AddVariable("in8", StorageType.Input);
            variableTableInterface.AddVariable("in9", StorageType.Input);
            variableTableInterface.AddVariable("outVal", StorageType.Output);

            in1 = variableTableInterface.GetVariableValue<T>("in1");
            in2 = variableTableInterface.GetVariableValue<T>("in2");
            in3 = variableTableInterface.GetVariableValue<T>("in3");
            in4 = variableTableInterface.GetVariableValue<T>("in4");
            in5 = variableTableInterface.GetVariableValue<T>("in5");
            in6 = variableTableInterface.GetVariableValue<T>("in6");
            in7 = variableTableInterface.GetVariableValue<T>("in7");
            in8 = variableTableInterface.GetVariableValue<T>("in8");
            in9 = variableTableInterface.GetVariableValue<T>("in9");
        }
        public override IPlcMember GetInterfaceVariableByName(string name)
        {
            if (name == "out") return OUT;
            else return variableTableInterface.GetVariableValue<IPlcMember>(name);
        }

        //先支持9个，太多了好麻烦....
        public bool AddValue()
        {
            if (OUT == null)
            {
                return false;
            }
            if (OUT is PlcInt IntVal)
            {
                if (_inCount >= 2)
                {
                    if (in1 != null && in2 != null)
                    {
                        if (in1 is PlcInt in1Int && in2 is PlcInt in2Int)
                        {
                            IntVal.Value = (short)(in1Int.Value + in2Int.Value);
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 3)
                {
                    if(in3!=null && in3 is PlcInt in3Int)
                    {
                        IntVal.Value = (short)(IntVal.Value + in3Int.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if(_inCount >= 4)
                {
                    if (in4 != null && in4 is PlcInt in4Int)
                    {
                        IntVal.Value = (short)(IntVal.Value + in4Int.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if(_inCount >= 5)
                {
                    if (in5 != null && in5 is PlcInt in5Int)
                    {
                        IntVal.Value = (short)(IntVal.Value + in5Int.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if(_inCount >= 6)
                {
                    if (in6 != null && in6 is PlcInt in6Int)
                    {
                        IntVal.Value = (short)(IntVal.Value + in6Int.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if(_inCount >= 7)
                {
                    if (in7 != null && in7 is PlcInt in7Int)
                    {
                        IntVal.Value = (short)(IntVal.Value + in7Int.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if(_inCount >= 8)
                {
                    if (in8 != null && in8 is PlcInt in8Int)
                    {
                        IntVal.Value = (short)(IntVal.Value + in8Int.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if(_inCount >= 9)
                {
                    if (in9 != null && in9 is PlcInt in9Int)
                    {
                        IntVal.Value = (short)(IntVal.Value + in9Int.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else if (OUT is PlcSInt sIntVal)
            {
                if (_inCount >= 2)
                {
                    if (in1 != null && in2 != null)
                    {
                        if (in1 is PlcSInt in1SInt && in2 is PlcSInt in2SInt)
                        {
                            sIntVal.Value = (sbyte)(in1SInt.Value + in2SInt.Value);
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 3)
                {
                    if (in3 != null && in3 is PlcSInt in3SInt)
                    {
                        sIntVal.Value = (sbyte)(sIntVal.Value + in3SInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 4)
                {
                    if (in4 != null && in4 is PlcSInt in4SInt)
                    {
                        sIntVal.Value = (sbyte)(sIntVal.Value + in4SInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 5)
                {
                    if (in5 != null && in5 is PlcSInt in5SInt)
                    {
                        sIntVal.Value = (sbyte)(sIntVal.Value + in5SInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 6)
                {
                    if (in6 != null && in6 is PlcSInt in6SInt)
                    {
                        sIntVal.Value = (sbyte)(sIntVal.Value + in6SInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 7)
                {
                    if (in7 != null && in7 is PlcSInt in7SInt)
                    {
                        sIntVal.Value = (sbyte)(sIntVal.Value + in7SInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 8)
                {
                    if (in8 != null && in8 is PlcSInt in8SInt)
                    {
                        sIntVal.Value = (sbyte)(sIntVal.Value + in8SInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 9)
                {
                    if (in9 != null && in9 is PlcSInt in9SInt)
                    {
                        sIntVal.Value = (sbyte)(sIntVal.Value + in9SInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else if (OUT is PlcUInt uIntVal) {
            if (_inCount >= 2)
                {
                    if (in1 != null && in2 != null)
                    {
                        if (in1 is PlcUInt in1UInt && in2 is PlcUInt in2UInt)
                        {
                            uIntVal.Value = (ushort)(in1UInt.Value + in2UInt.Value);
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 3)
                {
                    if (in3 != null && in3 is PlcUInt in3UInt)
                    {
                        uIntVal.Value = (ushort)(uIntVal.Value + in3UInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 4)
                {
                    if (in4 != null && in4 is PlcUInt in4UInt)
                    {
                        uIntVal.Value = (ushort)(uIntVal.Value + in4UInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 5)
                {
                    if (in5 != null && in5 is PlcUInt in5UInt)
                    {
                        uIntVal.Value = (ushort)(uIntVal.Value + in5UInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 6)
                {
                    if (in6 != null && in6 is PlcUInt in6UInt)
                    {
                        uIntVal.Value = (ushort)(uIntVal.Value + in6UInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 7)
                {
                    if (in7 != null && in7 is PlcUInt in7UInt)
                    {
                        uIntVal.Value = (ushort)(uIntVal.Value + in7UInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 8)
                {
                    if (in8 != null && in8 is PlcUInt in8UInt)
                    {
                        uIntVal.Value = (ushort)(uIntVal.Value + in8UInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 9)
                {
                    if (in9 != null && in9 is PlcUInt in9UInt)
                    {
                        uIntVal.Value = (ushort)(uIntVal.Value + in9UInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else if (OUT is PlcUDInt uDintVal) {
            if (_inCount >= 2)
                {
                    if (in1 != null && in2 != null)
                    {
                        if (in1 is PlcUDInt in1UDInt && in2 is PlcUDInt in2UDInt)
                        {
                            uDintVal.Value = (uint)(in1UDInt.Value + in2UDInt.Value);
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 3)
                {
                    if (in3 != null && in3 is PlcUDInt in3UDInt)
                    {
                        uDintVal.Value = (uint)(uDintVal.Value + in3UDInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 4)
                {
                    if (in4 != null && in4 is PlcUDInt in4UDInt)
                    {
                        uDintVal.Value = (uint)(uDintVal.Value + in4UDInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 5)
                {
                    if (in5 != null && in5 is PlcUDInt in5UDInt)
                    {
                        uDintVal.Value = (uint)(uDintVal.Value + in5UDInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 6)
                {
                    if (in6 != null && in6 is PlcUDInt in6UDInt)
                    {
                        uDintVal.Value = (uint)(uDintVal.Value + in6UDInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 7)
                {
                    if (in7 != null && in7 is PlcUDInt in7UDInt)
                    {
                        uDintVal.Value = (uint)(uDintVal.Value + in7UDInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 8)
                {
                    if (in8 != null && in8 is PlcUDInt in8UDInt)
                    {
                        uDintVal.Value = (uint)(uDintVal.Value + in8UDInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 9)
                {
                    if (in9 != null && in9 is PlcUDInt in9UDInt)
                    {
                        uDintVal.Value = (uint)(uDintVal.Value + in9UDInt.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else if (OUT is PlcReal RealVal) {
            if (_inCount >= 2)
                {
                    if (in1 != null && in2 != null)
                    {
                        if (in1 is PlcReal in1Real && in2 is PlcReal in2Real)
                        {
                            RealVal.Value = (float)(in1Real.Value + in2Real.Value);
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 3)
                {
                    if (in3 != null && in3 is PlcReal in3Real)
                    {
                        RealVal.Value = (float)(RealVal.Value + in3Real.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 4)
                {
                    if (in4 != null && in4 is PlcReal in4Real)
                    {
                        RealVal.Value = (float)(RealVal.Value + in4Real.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 5)
                {
                    if (in5 != null && in5 is PlcReal in5Real)
                    {
                        RealVal.Value = (float)(RealVal.Value + in5Real.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 6)
                {
                    if (in6 != null && in6 is PlcReal in6Real)
                    {
                        RealVal.Value = (float)(RealVal.Value + in6Real.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 7)
                {
                    if (in7 != null && in7 is PlcReal in7Real)
                    {
                        RealVal.Value = (float)(RealVal.Value + in7Real.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 8)
                {
                    if (in8 != null && in8 is PlcReal in8Real)
                    {
                        RealVal.Value = (float)(RealVal.Value + in8Real.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >=9)
                {
                    if (in9 != null && in9 is PlcReal in9Real)
                    {
                        RealVal.Value = (float)(RealVal.Value + in9Real.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else if (OUT is PlcLReal lRealVal) {
            if (_inCount >= 2)
                {
                    if (in1 != null && in2 != null)
                    {
                        if (in1 is PlcLReal in1LReal && in2 is PlcLReal in2LReal)
                        {
                            lRealVal.Value = (double)(in1LReal.Value + in2LReal.Value);
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 3)
                {
                    if (in3 != null && in3 is PlcLReal in3LReal)
                    {
                        lRealVal.Value = (double)(lRealVal.Value + in3LReal.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 4)
                {
                    if (in4 != null && in4 is PlcLReal in4LReal)
                    {
                        lRealVal.Value = (double)(lRealVal.Value + in4LReal.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 5)
                {
                    if (in5 != null && in5 is PlcLReal in5LReal)
                    {
                        lRealVal.Value = (double)(lRealVal.Value + in5LReal.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 6)
                {
                    if (in6 != null && in6 is PlcLReal in6LReal)
                    {
                        lRealVal.Value = (double)(lRealVal.Value + in6LReal.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 7)
                {
                    if (in7 != null && in7 is PlcLReal in7LReal)
                    {
                        lRealVal.Value = (double)(lRealVal.Value + in7LReal.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 8)
                {
                    if (in8 != null && in8 is PlcLReal in8LReal)
                    {
                        lRealVal.Value = (double)(lRealVal.Value + in8LReal.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                if (_inCount >= 9)
                {
                    if (in9 != null && in9 is PlcLReal in9LReal)
                    {
                        lRealVal.Value = (double)(lRealVal.Value + in9LReal.Value);
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
            else
            {
                return false;
            }
            return false;
        }
    }
}
